package com.mszlu.news.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.admin.mapper.AuthenticationMapper;
import com.mszlu.news.admin.mapper.SysUserMapper;
import com.mszlu.news.admin.model.params.AuthenticationParam;
import com.mszlu.news.admin.pojo.Authentication;
import com.mszlu.news.admin.pojo.SysUser;
import com.mszlu.news.admin.service.AuthenticationService;
import com.mszlu.news.admin.vo.AuthenticationVo;
import com.mszlu.news.admin.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {
    @Autowired
    private AuthenticationMapper authenticationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result addAuthentication(AuthenticationParam authenticationParam) {
        QueryWrapper<Authentication> authenticationQueryWrapper=new QueryWrapper<>();
        authenticationQueryWrapper.eq("user_id",authenticationParam.getUserId());
        if(authenticationMapper.selectOne(authenticationQueryWrapper)!=null){
            return Result.success(null);
        }
        Authentication authentication=new Authentication();
        authentication.setUserId(authenticationParam.getUserId());
        authentication.setName(authenticationParam.getName());
        authentication.setMobilePhoneNumber(authenticationParam.getMobilePhoneNumber());
        authentication.setEmail(authenticationParam.getEmail());
        authentication.setAddress(authenticationParam.getAddress());
        authentication.setType(authenticationParam.getType());
        authentication.setImg(authenticationParam.getImg());
        SysUser sysUser=new SysUser();
        sysUser.setId(authenticationParam.getUserId());
        sysUser.setV(2);//使V认证状态变为2进行中
        sysUserMapper.updateById(sysUser);
        authenticationMapper.insert(authentication);
        return Result.success("提交认证申请成功");
    }

    @Override
    public Result selectAuthentication(AuthenticationParam authenticationParam) {
        Page<AuthenticationVo> authenticationPage=new Page<>(authenticationParam.getPage(), authenticationParam.getPageSize());
        IPage<AuthenticationVo> authenticationVoIPage=authenticationMapper.selectAuthentication(authenticationPage, authenticationParam.getUserId(), authenticationParam.getAccount(), authenticationParam.getName(), authenticationParam.getType());
        return Result.success(authenticationVoIPage);
    }

    @Override
    public Result deleteAuthentication(AuthenticationParam authenticationParam) {
        SysUser sysUser=new SysUser();
        sysUser.setId(authenticationParam.getUserId());
        sysUser.setV(1);
        sysUserMapper.updateById(sysUser);
        authenticationMapper.deleteById(authenticationParam.getId());
        return Result.success("删除成功");
    }

    @Override
    public Result admitAuthentication(AuthenticationParam authenticationParam) {
        SysUser sysUser=new SysUser();
        sysUser.setId(authenticationParam.getUserId());
        sysUser.setV(3);
        sysUserMapper.updateById(sysUser);
        authenticationMapper.deleteById(authenticationParam.getId());
        return Result.success("认证通过");
    }
}
